package leetcode.map;

import java.util.HashMap;

public class Lc2352 {

    public int equalPairs(int[][] grid) {
        HashMap<String, Integer> map = new HashMap<>();
        for (int i = 0; i < grid.length; i++) {
            StringBuilder num = new StringBuilder();
            for (int j = 0; j < grid[i].length; j++) {
                num.append(grid[i][j]);
                num.append("-");
            }
            map.merge(num.toString(), 1, Integer::sum);
        }

        int ans = 0;
        for (int i = 0; i < grid.length; i++) {
            StringBuilder num = new StringBuilder();
            for (int j = 0; j < grid[i].length; j++) {
                num.append(grid[j][i]);
                num.append("-");
            }
            ans += map.getOrDefault(num.toString(), 0);
        }
        return ans;
    }

}
